package com.dongzili.postgresql.common.satoken;

import cn.dev33.satoken.dao.SaTokenDaoForRedisTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * sa-token相关数据，使用统一前缀存储进redis
 *  <h3>具体实现步骤：</h3>
 *  <ul>
 *      <li>
 *          第一步：排除{@link SaTokenDaoForRedisTemplate}自动装配。如通过配置文件排除：
 *          {@code
 *           spring:
 *             autoconfigure:
 *               exclude:
 *                 - cn.dev33.satoken.dao.SaTokenDaoRedisJackson
 *          }
 *      </li>
 *      <li>
 *          第二步：重写SaTokenDaoRedisJackson并注册进spring，参考这里的{@link RedisPrefixSaTokenDaoRedisJackson}
 *      </li>
 *  </ul>
 *
 */
@Component
public class RedisPrefixSaTokenDaoRedisJackson extends SaTokenDaoForRedisTemplate {
        
//    private static final String REDIS_CACHE_PREFIX = "project-name:";
    @Value("${sa-token.redis.token-name-prefix}")
    private String REDIS_CACHE_PREFIX;

//    private final String prefix;
//
//    public RedisPrefixSaTokenDaoRedisJackson(){
//        prefix = appendCachePrefix(REDIS_CACHE_PREFIX);
//    }

    @Override
    public String get(String key) {
        key = appendCachePrefix(key);
        return super.get(key);
    }
    
    @Override
    public void set(String key, String value, long timeout) {
        key = appendCachePrefix(key);
        super.set(key, value, timeout);
    }
    
    @Override
    public void update(String key, String value) {
        key = appendCachePrefix(key);
        super.update(key, value);
    }
    
    @Override
    public void delete(String key) {
        key = appendCachePrefix(key);
        super.delete(key);
    }
    
    @Override
    public long getTimeout(String key) {
        key = appendCachePrefix(key);
        return super.getTimeout(key);
    }
    
    @Override
    public void updateTimeout(String key, long timeout) {
        key = appendCachePrefix(key);
        super.updateTimeout(key, timeout);
    }
    
    @Override
    public Object getObject(String key) {
        key = appendCachePrefix(key);
        return super.getObject(key);
    }
    
    @Override
    public void setObject(String key, Object object, long timeout) {
        key = appendCachePrefix(key);
        super.setObject(key, object, timeout);
    }
    
    @Override
    public void updateObject(String key, Object object) {
        key = appendCachePrefix(key);
        super.updateObject(key, object);
    }
    
    @Override
    public void deleteObject(String key) {
        key = appendCachePrefix(key);
        super.deleteObject(key);
    }
    
    @Override
    public long getObjectTimeout(String key) {
        key = appendCachePrefix(key);
        return super.getObjectTimeout(key);
    }
    
    @Override
    public void updateObjectTimeout(String key, long timeout) {
        key = appendCachePrefix(key);
        super.updateObjectTimeout(key, timeout);
    }
    
    @Override
    public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) {
        prefix = appendCachePrefix(prefix);
        return super.searchData(prefix, keyword, start, size, sortType);
    }
    
    /**
     * 增加sa-token相关redis cache前缀
     */
    private String appendCachePrefix(String key) {
        if (key == null) {
            return null;
        }
        if (key.startsWith(REDIS_CACHE_PREFIX)) {
            return key;
        }
        return REDIS_CACHE_PREFIX +":"+ key;
    }
}